package cn.codesugar.advance.utils

import android.util.Log
import cn.codesugar.advance.MyApplication

object LogUtil {

    private var level = LogLevel.VERBOSE

    fun setLevel(logLevel: LogLevel){
        this.level = logLevel
    }

    fun getLevel(): LogLevel = level

    fun v(tag: String, msg: String) {
        if (level <= LogLevel.VERBOSE) {
            Log.v(tag, msg)
        }
    }

    fun d(tag: String, msg: String) {
        if (level <= LogLevel.DEBUG) {
            Log.d(tag, msg)
        }
    }

    fun i(tag: String, msg: String) {
        if (level <= LogLevel.INFO) {
            Log.i(tag, msg)
        }
    }

    fun w(tag: String, msg: String) {
        if (level <= LogLevel.WARN) {
            Log.w(tag, msg)
        }
    }

    fun e(tag: String, msg: String) {
        if (level <= LogLevel.ERROR) {
            Log.e(tag, msg)
        }
    }

    fun a(tag: String, msg: String) {
        if (level <= LogLevel.ASSERT) {
            Log.wtf(tag, msg)
        }
    }

}

enum class LogLevel(value: Int) {
    VERBOSE(2),
    DEBUG(3),
    INFO(4),
    WARN(5),
    ERROR(6),
    ASSERT(7)
}

private val packageName = MyApplication.context.packageName

fun String.logVerbose(TAG: String = packageName) {
    if(LogUtil.getLevel() <= LogLevel.VERBOSE) {
        Log.v(TAG, this)
    }
}

fun String.logDebug(TAG: String = packageName) {
    if(LogUtil.getLevel() <= LogLevel.DEBUG) {
        Log.d(TAG, this)
    }
}

fun String.logInfo(TAG: String = packageName) {
    if(LogUtil.getLevel() <= LogLevel.INFO) {
        Log.i(TAG, this)
    }
}

fun String.logWarn(TAG: String = packageName) {
    if(LogUtil.getLevel() <= LogLevel.WARN) {
        Log.w(TAG, this)
    }
}

fun String.logError(TAG: String = packageName) {
    if(LogUtil.getLevel() <= LogLevel.ERROR) {
        Log.e(TAG, this)
    }
}

fun String.logAssert(TAG: String = packageName) {
    if(LogUtil.getLevel() <= LogLevel.ASSERT) {
        Log.wtf(TAG, this)
    }
}